package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.YdRecordCategoryT;
import com.jshoperxms.entity.YdRecordRdT;
import com.jshoperxms.service.YdRecordCategoryTService;
import com.jshoperxms.service.YdRecordRdTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hibernate.criterion.Restrictions.ne;

/**
 * Created by sdywcd on 2019/1/20.
 * Des:
 */
@RestController
@RequestMapping("/api/minecenter/records")
public class MineRecordsController extends BaseTController {

    @Resource
    private YdRecordCategoryTService ydRecordCategoryTService;

    @Resource
    private YdRecordRdTService ydRecordRdTService;

    /**
     * 获取用户的录音分类信息
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMineRdCategorys",method= RequestMethod.POST)
    public ResponseEntity findMineRdCategorys(@RequestBody YdRecordCategoryT rq){
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getWxopenid())) {
            Criterion criterion= Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
            List<YdRecordCategoryT>list=this.ydRecordCategoryTService.findByCriteria(YdRecordCategoryT.class,criterion);
            responsedata.put(TagWords.BEANLIST,list);
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS, false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    @RequestMapping(value="/filterMineYdRecords",method= RequestMethod.POST)
    public ResponseEntity filterMineYdRecords(@RequestBody SearchModel rq){
        Map<String, Object> responsedata = new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())){
            //如果是默认全部的情况
            Criterion criterion=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
            int currentPage=rq.getStart();
            int lineSize=rq.getLength();
            if(StringUtils.isNotBlank(rq.getRdcategoryid())&&!StringUtils.equals(rq.getRdcategoryid(),"0")){
                criterion=Restrictions.and(criterion).add(Restrictions.eq("rdcategoryid",rq.getRdcategoryid()));
            }
            if(rq.getRddurationmin()!=0&&rq.getRddurationmax()!=0){
                criterion=Restrictions.and(criterion).add(Restrictions.between("duration",rq.getRddurationmin(),rq.getRddurationmax()));
            }
            int records=this.ydRecordRdTService.count(YdRecordRdT.class,criterion).intValue();
            List<YdRecordRdT> list=this.ydRecordRdTService.findByCriteriaByPage(YdRecordRdT.class,criterion, Order.desc(TagWords.CREATETIME),currentPage,lineSize);
            rq.setRecordsTotal(records);
            responsedata.put("results",list);
            responsedata.put("start",rq.getNextPageNo());
            responsedata.put("totalPage",rq.getTotalPage());
            responsedata.put("totalrecords",records);
            responsedata.put(TagWords.SUCCESS,true);
        }
        return Response.success(responsedata);

    }


}
